package  
{
  import flash.display.DisplayObjectContainer;
  import flash.geom.Point;
  import mge.particles.Descriptor;
  import mge.particles.ParticleSystem;
  import mge.serialization.Serializer;
	/**
   * ...
   * @author Ivan
   */
  public class Particles 
  {
    [Embed(source="C:/lib/mge/projects/particle_editor/resources/particles.png")]
    private var rParticles:Class;
    
    [Embed(source='../assets/particles/jet.xml',mimeType="application/octet-stream")]
    private var rJetPS: Class;
    
    [Embed(source='../assets/particles/smoke.xml',mimeType="application/octet-stream")]
    private var rSmokePS: Class;
    
    public function Particles() 
    {
      jet = new ParticleSystem(new rParticles);      
      jet.descriptor = new Serializer().deserialize(new XML(new rJetPS()), Descriptor) as Descriptor;
      
      smokes = new Vector.<ParticleSystem>();
    }
    
    public function startSmoke(parent:DisplayObjectContainer, position:Point):void
    {
      var ps:ParticleSystem = new ParticleSystem(new rParticles);
      
      ps.descriptor =new Serializer().deserialize(new XML(new rSmokePS()), Descriptor) as Descriptor;
      parent.addChild(ps);
      ps.descriptor.position.x = position.x;
      ps.descriptor.position.y = position.y;
      ps.fire();
      smokes.push(ps);
    }
    
    public function update():void
    {
      jet.update();
      
      for (var i:int = 0; i < smokes.length; i++)
      {
        var ps:ParticleSystem = smokes[i];
        ps.update();
        if (ps.finished)
        {
          ps.parent.removeChild(ps);
          smokes.splice(i, 1);
        }
      }
    }
    
    public var jet:ParticleSystem;
    private var smoke:ParticleSystem;
    
    private var smokes:Vector.<ParticleSystem>;
    
  }

}